import java.util.*;

public class Solution {
    public int scoreOfParentheses(String s) {
        if (s.length() == 2) {
            return 1;
        }
        int bal = 0, n = s.length(), len = 0;
        for (int i = 0; i < n; i++) {
            bal += (s.charAt(i) == '(' ? 1 : -1);
            if (bal == 0) {
                len = i + 1;
                break;
            }
        }
        if (len == n) {
            return 2 * scoreOfParentheses(s.substring(1, n - 1));
        } else {
            return scoreOfParentheses(s.substring(0, len)) + scoreOfParentheses(s.substring(len));
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] testCases = {
                "()",           // 1
                "(())",         // 2
                "()()",         // 2
                "(()(()))",     // 6
                "((()))",       // 4
                "(()()())"      // 6
        };
        for (String test : testCases) {
            int score = solution.scoreOfParentheses(test);
            System.out.println("Input: \"" + test + "\" -> Score: " + score);
        }
        if (args.length > 0) {
            String input = args[0];
            int score = solution.scoreOfParentheses(input);
            System.out.println("\nCustom input: \"" + input + "\" -> Score: " + score);
        }
    }
}